package com.oracle.intelagr.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oracle.intelagr.common.JsonResult;
import com.oracle.intelagr.common.PageModel;
import com.oracle.intelagr.entity.BSoilmonitoring;
import com.oracle.intelagr.entity.MMonitorpoint;
import com.oracle.intelagr.entity.User;
import com.oracle.intelagr.service.IMonnitorPointService;
import com.oracle.intelagr.service.ISoilmoniService;

@Controller
@RequestMapping("soilMoni")
public class SoilMoniController {
	@Autowired
	private  ISoilmoniService soilmoniService;
	//处理查询请求的方法
	@RequestMapping("/list")
	public String list(@DateTimeFormat(pattern="yyyy-MM-dd")Date beginDate,@DateTimeFormat(pattern="yyyy-MM-dd")Date  endDate,PageModel pageModel,Map map) {
		//接受请求参数调用service获得结果将结果传给视图层，向视图层跳转，
		
		Map paramMap = new HashMap();
		paramMap.put("startMoniorDate", beginDate);
		paramMap.put("endMoniorDate", endDate);
		soilmoniService.query(pageModel, paramMap);
		map.put("pageModel", pageModel);
		map.put("beginDate", beginDate);
		map.put("endDate", endDate);
		SimpleDateFormat dateFormat2=new SimpleDateFormat("yyyy-MM-dd");
		if(beginDate!=null) {
			map.put("beginDate", dateFormat2.format(beginDate));
		}
		if(endDate!=null) {
			map.put("endDate", dateFormat2.format(endDate));
		}
		return "/soilmoni/soilMoniList";
	}
	@Autowired
	private IMonnitorPointService monitorPointService;
	@RequestMapping("/add")
	public String add(Map map) {
		List<MMonitorpoint> queryAll = monitorPointService.queryAll();
		map.put("moniPointList", queryAll);		
		return "/soilmoni/soilMoniAdd";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public JsonResult save(@RequestBody BSoilmonitoring soil,HttpServletRequest request) {
		soil.setDeleteflag("N");
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		soil.setCreateuserid(user.getUserID());
		soil.setCreatedate(new Date());
		soil.setUpdateuserid(user.getUserID());
		soil.setUpdatedate(new Date());
		soilmoniService.save(soil);
		//respobsebody会把它编程json属性 { status:200,msg:null,data:null}
		return new JsonResult(true);
	}
	@RequestMapping("/delete")
	@ResponseBody
	public JsonResult delete(@RequestBody int []ids,HttpServletRequest request) {
		/*int[] ids = new int[list.size()];
		for(int i=0;i<list.size();i++) {
			ids[i] = list.get(i);
		}*/
		soilmoniService.delete(ids);
		return new JsonResult(true);
	}
	@RequestMapping("/edit")
	public String edit(int id,Map map) {
		BSoilmonitoring queryById = soilmoniService.queryById(id);
		SimpleDateFormat dateFormat2=new SimpleDateFormat("yyyy-MM-dd");
		String format = dateFormat2.format(queryById.getMonitordate());
		map.put("monidate", format);
		map.put("soil", queryById);
		List<MMonitorpoint> queryAll = monitorPointService.queryAll();
		map.put("moniPointList", queryAll);		
		return "/soilmoni/soilMoniEdit";
	}
	@RequestMapping("/update")
	@ResponseBody
	public JsonResult update(@RequestBody BSoilmonitoring soil) {
		soilmoniService.update(soil);
		return new JsonResult(true);
	}
}
